package com.example.enterprise.service;


import com.example.enterprise.dao.SeLogMapper;
import com.example.enterprise.entity.SeLogExample;
import com.example.enterprise.entity.SeLogWithBLOBs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.example.enterprise.util.EnterpriseConstant.*;


@Service
public class SeLogService {


    @Autowired
    private SeLogMapper seLogMapper;

    //根据id,ISNOTDraft
    public List<SeLogWithBLOBs> findSeLogs(int enterpriseId, int offset, int limit) {
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISNOTDraft);
        example.setOrderByClause("check_time desc limit " + offset + "," + limit);
        List<SeLogWithBLOBs> seLogWithBLOBs = seLogMapper.selectByExampleWithBLOBs(example);

        return seLogWithBLOBs;
    }
    //根据id,ISNOTDraft
    public int findSeLogRows(int enterpriseId) {
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISNOTDraft);

        return (int) seLogMapper.countByExample(example);

    }
    //根据id,ISNOTDraft
    public List<SeLogWithBLOBs> findSeLogsByDate(int enterpriseId, int offset, int limit, Date start, Date end) {
        SeLogExample example = new SeLogExample();
        example.createCriteria().andCheckTimeBetween(start, end).andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISNOTDraft);
        example.setOrderByClause("check_time desc limit " + offset + "," + limit);

        List<SeLogWithBLOBs> seLogWithBLOBs = seLogMapper.selectByExampleWithBLOBs(example);

        return seLogWithBLOBs;
    }
    //根据id,ISNOTDraft
    public int findSeLogRowsByDate(int enterpriseId, Date start, Date end) {
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andCheckTimeBetween(start, end).andDraftEqualTo(ISNOTDraft);
        return (int) seLogMapper.countByExample(example);
    }

    public int insertSeLog(SeLogWithBLOBs seLogWithBLOBs) {
        return seLogMapper.insertSelective(seLogWithBLOBs);
    }

//    //查询是否有草稿
//    public List<SeLogWithBLOBs> findIsDraft(int enterpriseId) {
//        SeLogExample example = new SeLogExample();
//        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(1);
//
//        return seLogMapper.selectByExampleWithBLOBs(example);
//    }
//
//    //作废安全日志草稿
//    public int changeDraft(int seLogId) {
//
//        SeLogWithBLOBs draft = seLogMapper.selectByPrimaryKey(seLogId);
//        draft.setDraft(CHANGE_Draft);
//        return seLogMapper.updateByPrimaryKeyWithBLOBs(draft);
//    }

    //查找该企业的安全日志是否有草稿了
    public boolean haveSeLogDraft(int enterpriseId) {
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISDraft).andTypeEqualTo(Security_Log);
        List<SeLogWithBLOBs> seLogWithBLOBs = seLogMapper.selectByExampleWithBLOBs(example);

        if(seLogWithBLOBs.size()>0){
            return true;
        }
        else return false;

    }
    //改变安全日志状态
    public int changeSeLogDraft(int enterpriseId){
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISDraft).andTypeEqualTo(Security_Log);
        List<SeLogWithBLOBs> seLogWithBLOBs = seLogMapper.selectByExampleWithBLOBs(example);
        SeLogWithBLOBs draft=seLogWithBLOBs.get(0);
        draft.setDraft(ISNOTDraft);
        return seLogMapper.updateByPrimaryKeyWithBLOBs(draft);
    }
    //查找安全日志的草稿
    public List<SeLogWithBLOBs> findSeLogDraft(int enterpriseId){
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISDraft).andTypeEqualTo(Security_Log);
        return seLogMapper.selectByExampleWithBLOBs(example);
    }
    //根据log的id查询，用于展示
    public SeLogWithBLOBs findLogByID(int id){

        return seLogMapper.selectByPrimaryKey(id);
    }

    //类型：增加隐患log
    //查找该企业的增加隐患log是否有草稿了
    public boolean haveDangerLogDraft(int enterpriseId) {
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISDraft).andTypeEqualTo(Hidden_Danger);
        List<SeLogWithBLOBs> seLogWithBLOBs = seLogMapper.selectByExampleWithBLOBs(example);

        if(seLogWithBLOBs.size()>0){
            return true;
        }
        else return false;

    }
    //改变增加隐患log状态
    public int changeDangerLogDraft(int enterpriseId){
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISDraft).andTypeEqualTo(Hidden_Danger);
        List<SeLogWithBLOBs> seLogWithBLOBs = seLogMapper.selectByExampleWithBLOBs(example);
        SeLogWithBLOBs draft=seLogWithBLOBs.get(0);
        draft.setDraft(CHANGE_Draft);
        return seLogMapper.updateByPrimaryKeyWithBLOBs(draft);
    }
    //查找增加隐患log的草稿
    public List<SeLogWithBLOBs> findDangerLogDraft(int enterpriseId){
        SeLogExample example = new SeLogExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andDraftEqualTo(ISDraft).andTypeEqualTo(Hidden_Danger);
        return seLogMapper.selectByExampleWithBLOBs(example);
    }



}
